package com.atguigu.ssyx.acl.controller;

import com.atguigu.ssyx.acl.service.AdminService;
import com.atguigu.ssyx.acl.service.RoleService;
import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.common.utils.MD5;
import com.atguigu.ssyx.model.acl.Admin;
import com.atguigu.ssyx.vo.acl.AdminQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Api(tags = "用户管理")
@RestController
@RequestMapping("/admin/acl/user")
public class AdminController {

    @Resource
    private AdminService adminService;

    @Resource
    private RoleService roleService;

    // 用户列表（条件分页查询）
    @ApiOperation("用户列表")
    @GetMapping("{current}/{limit}")
    public Result list(@PathVariable Long current,
                       @PathVariable Long limit,
                       AdminQueryVo adminQueryVo) {
        Page<Admin> page = new Page<>(current, limit);
        IPage<Admin> pageModel = adminService.selectAdminPage(page, adminQueryVo);
        return Result.ok(pageModel);
    }

    // 根据id查询用户
    @ApiOperation("根据id查询用户")
    @GetMapping("get/{id}")
    public Result getAdmin(@PathVariable Long id) {
        Admin admin = adminService.getById(id);
        return Result.ok(admin);
    }

    // 添加用户
    @ApiOperation("添加用户")
    @PostMapping("save")
    public Result save(@RequestBody Admin admin) {
        // 获取输入的密码
        String password = admin.getPassword();
        // 密码MD5加密
        String md5Password = MD5.encrypt(password);
        // 设置到admin对象
        admin.setPassword(md5Password);
        // 调用service添加
        boolean save = adminService.save(admin);
        return Result.ok(save);
    }

    // 修改用户
    @ApiOperation("修改用户")
    @PutMapping("update")
    public Result update(@RequestBody Admin admin) {
        boolean update = adminService.updateById(admin);
        return Result.ok(update);
    }

    // 根据id删除用户
    @ApiOperation("根据id删除用户")
    @DeleteMapping("remove/{id}")
    public Result removeById(@PathVariable Long id) {
        boolean remove = adminService.removeById(id);
        return Result.ok(remove);
    }

    // 根据id批量删除用户
    @ApiOperation("根据id批量删除用户")
    @DeleteMapping("batchRemove")
    public Result batchRemove(@RequestBody List<Long> ids) {
        boolean batchRemove = adminService.removeByIds(ids);
        return Result.ok(batchRemove);
    }

    // 获取所有角色，根据id获取用户已分配角色列表
    @ApiOperation("获取用户角色")
    @GetMapping("toAssign/{userId}")
    public Result toAssign(@PathVariable Long userId) {
        Map<String, Object> roleMap = roleService.findRoleByUserId(userId);
        return Result.ok(roleMap);
    }

    // 角色分配, 为用户分配角色:参数：用户id，多个角色id
    @ApiOperation("为用户分配角色")
    @PostMapping("doAssign")
    public Result doAssign(@RequestParam Long adminId, @RequestParam List<Long> roleId) {
        roleService.saveAdminRole(adminId, roleId);
        return Result.ok(null);
    }
}
